﻿using System;
using System.Collections.Generic;
using System.Text;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.DataAccess.SQLDom;
using T.FAS.Runtime.Metadata.Entity;

namespace T.FAS.Runtime.DataAccess.SPI
{
    public class InvokeContext: AuthControlContext
    {
        private string uniqueIdentifier;
        /// <summary>
        /// 唯一标识，调试用
        /// </summary>
        public string UniqueIdentifier
        {
            get
            {
                if (string.IsNullOrEmpty(uniqueIdentifier))
                {
                    uniqueIdentifier = Guid.NewGuid().ToString();
                }
                return uniqueIdentifier;
            }
        }
        /// <summary>
        /// 元数据类型: DataObject, DataQuery, EntityObject
        /// </summary>
        public string MetaType { get; set; }
        /// <summary>
        /// 元数据定义
        /// </summary>
        public IMetaDataContent SourceMeta { get; set; }
        /// <summary>
        /// CDO 节点id
        /// </summary>
        public string NodeID { get; set; }
        /// <summary>
        /// 入参数据 For Save Update
        /// </summary>
        public TRecord SourceData { get; set; }
        /// <summary>
        /// 入参主键 For Delete Query
        /// </summary>
        public string SourceDataID { get; set; }
        /// <summary>
        /// 入参主键集合 For Delete Query
        /// </summary>
        public List<string> SourceDataIDs { get; set; }
        /// <summary>
        /// 扩展参数
        /// </summary>
        public Dictionary<string, object> ExtProperties { get; set; } = new Dictionary<string, object>();
        /// <summary>
        /// 插件数据
        /// </summary>
        private Dictionary<string, Dictionary<string, object>> PluginData { get; set; } = new Dictionary<string, Dictionary<string, object>>();
        /// <summary>
        /// 是否使用默认结果处理器
        /// </summary>
        public bool UseDefulatResultHandler { get;set; }
        /// <summary>
        /// 当前环境执行记录
        /// </summary>
        public List<ActionResult> ExecuteLogs => new List<ActionResult>();

        /// <summary>
        /// 向上下文中写入返回数据，以插件标识区分
        /// </summary>
        /// <param name="pluginCode"></param>
        /// <param name="data"></param>
        public void RecordData(string pluginCode, string dataKey ,object data)
        {
            if (!PluginData.ContainsKey(pluginCode))
            {
                PluginData[pluginCode] = new Dictionary<string, object>();
            }
            PluginData[pluginCode][dataKey] = data;
        }

        /// <summary>
        /// 从上下文中取出数据
        /// </summary>
        /// <param name="pluginCode"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetData(string pluginCode, string key)
        {
            if (!PluginData.ContainsKey(pluginCode))
            {
                return null;
            }
            if (PluginData[pluginCode] == null || !PluginData[pluginCode].ContainsKey(key))
            {
                return null;
            }
            return PluginData[pluginCode][key];
        }

        /// <summary>
        /// 根据字典指定的key同步到上下文的字典中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exrParams"></param>
        /// <param name="key"></param>
        public void InitExtProps<T>(Dictionary<string, object> exrParams, string key)
        {
            if (exrParams != null && exrParams.ContainsKey(key))
            {
                if (typeof(T) == typeof(bool))
                {
                    if (exrParams[key]?.ToString().ToLower() == "true")
                    {
                        this.ExtProperties[key] = true;
                    }
                }
            }
        }
    }
}
